package com.qingfeng.music.ui.main.activity;

import android.Manifest;
import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.qingfeng.music.Constant;
import com.qingfeng.music.R;
import com.qingfeng.music.service.ScanMusicService;
import com.qingfeng.music.service.connection.BaseServiceConnection;
import com.shuji.android.library.BaseActivity;
import com.shuji.android.library.utils.EasyPermissions;

import java.util.List;

import butterknife.Bind;

/**
 * 开场动画
 */
public class SplashActivity extends BaseActivity implements EasyPermissions.PermissionCallback {
    private static final String TAG = "SplashActivity";

    private static final int PERMS_REQUEST_CODE = 100;

    @Bind(R.id.logo)
    ImageView mImageView;

    @Bind(R.id.name)
    TextView mTextView;

    //---------------------------

    private String[] perms = {Manifest.permission.READ_EXTERNAL_STORAGE}; //读取权限


    //---------------------------------------------------------------
    private BaseServiceConnection<ScanMusicService> mScanMusicServiceConnection;//扫描歌曲服务
    private BroadcastReceiver mMusicScanReceiver; //扫描广告接收者


    //-----------------------------------------------
    @Override
    public int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {

        //沉浸式状态栏
        translucentStatusBar();

        //绑定扫描服务服务
        Intent intent = new Intent(this, ScanMusicService.class);
        mScanMusicServiceConnection = new BaseServiceConnection<>();
        bindService(intent, mScanMusicServiceConnection, Context.BIND_AUTO_CREATE);

        //注册广播接收器(扫描歌曲)
        mMusicScanReceiver = new MusicScanReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Constant.RECEIVER_MUSIC_SCAN_SUCCESS);
        registerReceiver(mMusicScanReceiver, intentFilter);

        //动画
        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0.3f, 1f);
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.3f, 1f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.3f, 1f);

        ObjectAnimator objectAnimator1 = ObjectAnimator.ofPropertyValuesHolder(mImageView, alpha, scaleX, scaleY); // 属性动画1
        ObjectAnimator objectAnimator2 = ObjectAnimator.ofPropertyValuesHolder(mTextView, alpha, scaleX, scaleY);  // 属性动画2

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(objectAnimator1, objectAnimator2); //同时播放动画
        animatorSet.setInterpolator(new AccelerateInterpolator()); //设置补间器
        animatorSet.setDuration(2000); //动画时长

        animatorSet.addListener(new Animator.AnimatorListener() { //  动画结束后，扫描本地歌曲
            @Override
            public void onAnimationStart(Animator animator) {

            }

            /**
             * 动画结束后，扫描本地歌曲
             *
             * @param animator
             */
            @Override
            public void onAnimationEnd(Animator animator) {
                if (!EasyPermissions.hasPermissions(SplashActivity.this, perms)) {
                    EasyPermissions.requestPermissions(SplashActivity.this, "扫描本地歌曲需要读取权限", PERMS_REQUEST_CODE, perms);
                } else {
                    mScanMusicServiceConnection.getService().scanMusic(); //开始扫描
                }
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

        animatorSet.start(); //开始动画
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unbindService(mScanMusicServiceConnection); //解帮服务
        unregisterReceiver(mMusicScanReceiver); //注销广播接收器
    }


    //------------------权限处理-------------------------------------------
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(PERMS_REQUEST_CODE, permissions, grantResults, SplashActivity.this);
    }


    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
        mScanMusicServiceConnection.getService().scanMusic();
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
        MainActivity.startAction(SplashActivity.this);
        finish();
    }

    //------------------------------------------------------------------------------

    /**
     * 接受广播： 扫描歌曲结束后，启动引导界面
     */
    private class MusicScanReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (Constant.RECEIVER_MUSIC_SCAN_SUCCESS.equals(intent.getAction())) {
                GuideActivity.startAction(SplashActivity.this);
                finish();
            }
        }
    }


}
